package com.hq.controller;

import com.hq.entity.QuestionPo;
import com.hq.entity.QuestionVo;
import com.hq.entity.ResultVo;
import com.hq.service.QuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/question")
@Slf4j
public class QuestionController {
    @Autowired
    private QuestionService questionService;

    //批量删除
    @RequestMapping("/dels")
    public ResultVo dels(String qids){
        ResultVo vo=new ResultVo();
        if(questionService.delQuestions(qids)>0){
            vo.setCode(200);
            vo.setMsg("修改成功！");
        }else{
            vo.setCode(500);
            vo.setMsg("修改失败！");
        }
        return vo;
    }

    //修改试卷
    @RequestMapping("/edit")
    public ResultVo edit(@RequestBody QuestionVo question){
        log.info("修改试卷：{}",question);
        ResultVo vo=new ResultVo();
        if(questionService.updateQuestion(question)>0){
            vo.setCode(200);
            vo.setMsg("修改成功！");
        }else{
            vo.setCode(500);
            vo.setMsg("修改失败！");
        }
        return vo;
    }

    //根据id查询试卷
    @RequestMapping("/get")
    public QuestionPo get(Integer qid){
        return questionService.findById(qid);
    }

    //试卷分页查询
    @RequestMapping("/list")
    public Map<String,Object> list(int page, int size){
        return questionService.findByPage(page,size);
    }

    //录入试卷
    @RequestMapping("/add")
    public ResultVo add(@RequestBody QuestionVo question){
        log.info("录入试卷：{}",question);
        ResultVo vo=new ResultVo();
        if(questionService.addQuestion(question)>0){
            vo.setCode(200);
            vo.setMsg("录入成功！");
        }else{
            vo.setCode(500);
            vo.setMsg("录入失败！");
        }
        return vo;
    }
    //单行删除试卷
    @RequestMapping("/del")
    public ResultVo del(int qid){
        log.info("删除试卷：{}",qid);
        ResultVo vo=new ResultVo();
        if(questionService.delQuestion(qid)>0){
            vo.setCode(200);
            vo.setMsg("删除成功！");
        }else{
            vo.setCode(500);
            vo.setMsg("删除失败！");
        }
        return vo;
    }

}